
package com.compuvest.admin.update.v1

import java.io.File;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.AlphaComposite;
import java.awt.image.BufferedImage;
import java.awt.geom.Rectangle2D;

import javax.imageio.ImageIO;

class Image {

    def final static WWWPATH = "/var/lighttpd/1.4/docroot/"

    def imgs = [:], pdf = [:], pictures, accwareid

    String getJson() {

        def array = new StringBuilder(), result, bimage, idx = 0
        pictures = []
        imgs.each { side, link ->

            bimage = getImage(link)
            //println "side=${side} link=${link} bimage=${bimage ? 'have':'empty' }"

            if(bimage) {
                result = makeImage(bimage, side)
                if(result) {
                    array << (idx++ == 0 ? '' : ',') << "\"" << side << "\""
                    pictures << side
                }
            }
        }

        return array.toString()
    }

    // for Nosql
    def getPictures = {
        pictures.join(',')
    }

    def getImage(http_link) {

        def bimage = null
        try {
            def url = new URL(http_link)

            // set time out
            URLConnection conn = url.openConnection()
            conn.setConnectTimeout(1000 * 2) // 2sec
            conn.setReadTimeout(1000 * 2)    // 2sec

            bimage = ImageIO.read(url)
        }
        catch(IOException ignore) { }

        return bimage
    }

    Boolean makeImage(bimage, side) {

        int width = 300
        int height = 300
        double defRatio = (double)width / (double)height

        int imageWidth = bimage.getWidth(null)
        int imageHeight = bimage.getHeight(null)
        double imageRatio = (double)imageWidth / (double)imageHeight

        int defWidth = width
        int defHeight = height
        if(defRatio < imageRatio) {
            defHeight = (int)(width / imageRatio)
        }
        else {
            defWidth = (int)(height * imageRatio)
        }

        def mold = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)

        def g2 = mold.createGraphics()
        g2.setColor(Color.white) // white background
        g2.fillRect(0, 0, width, height)
        g2.drawImage(bimage, 0, 0, defWidth, defHeight, null)

        g2.setColor(Color.LIGHT_GRAY)
        g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.2f)) // 20% transp
        g2.setFont(new Font("Arial", Font.BOLD, 16))

        //String watermark = "COMPUVEST.COM"
        String watermark = "."

        FontMetrics fontMetrics = g2.getFontMetrics()
        Rectangle2D rect = fontMetrics.getStringBounds(watermark, g2)

        g2.drawString(watermark, (width - (int)rect.getWidth()) / 2, (height - (int)rect.getHeight()) / 2)

        g2.dispose()

        try {
            ImageIO.write(mold, "jpg", new File(WWWPATH + side + "/" + accwareid + ".jpg"))
        }
        catch(IOException e) {
            return false
        }

        return true
    }


}
